package ru.autofan.logic;

import java.util.HashMap;
import java.util.Map;

import ru.autofan.domain.Role;

public class RoleService extends BasicService {

	/*
	 * If no role in DB than create and return new Role
	 */
	public <T extends Role> T getRole(String role, Class<T> roleClazz) {
		Map<String, Object> params = new HashMap<String, Object>(1);
		params.put("type", role);
		T result = null;
		try {
			result = getDaoSupporter().getUniqueObject(roleClazz,
					"from Role where type = :type", params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result == null) {

			try {
				result = (T) Class.forName(roleClazz.getCanonicalName())
						.newInstance();
				result.setType(role);
				getDomainFactory().save(result);
			} catch (Exception e) {
				return null;
			}
		}
		return result;
	}

}
